<?php
namespace app\common\model;

use app\common\library\Auth;
use think\Model;
use think\Db;
use traits\model\SoftDelete;

class Trends extends Model
{
	use SoftDelete;
	// 设置当前模型使用的完整数据库连接信息
	protected $connection = 'database.db_second';//连接配置
    // 表名
    protected $name = 'trends';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = false;

    // 定义时间戳字段名
    protected $createTime = false;
    protected $updateTime = false;
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
    	'avatar',
	    'nickname',
	    'is_like',
	    'goods_id',
	    'goods_title',
	    'is_coll',//收藏
        'state_text',
        'is_name_text',
        'is_see_text',
        'is_draft_text',
        'create_time_text',
        'huati_list',
        'hy_list',
	    'images_url',
	    'mp4_file_url'
    ];

	public function getNicknameAttr($value, $data){

		$value = Db::name("user")->where(['id'=>$data['user_id']])->value('nickname');

		return $value ? $value : '';

	}


	public function getAvatarAttr($value, $data){

		$value = Db::name("user")->where(['id'=>$data['user_id']])->value('avatar');

		return $value ? $value : '';

	}
    
    public function getStateList()
    {
        return ['1' => __('正常'),'4' => __('审核中'), '2' => __('封禁'), '3' => __('已驳回')];
    }

    public function getIsNameList()
    {
        return ['1' => __('Is_name 1'), '2' => __('Is_name 2')];
    }

    public function getIsSeeList()
    {
        return ['1' => __('Is_see 1'), '2' => __('Is_see 2')];
    }

    public function getIsDraftList()
    {
        return ['1' => __('Is_draft 1'), '2' => __('Is_draft 2')];
    }

//	public function identity()
//	{
//		return ['1' => __('吧主'), '2' => __('管理员'), '2' => __('圈友')];
//	}


    public function getStateTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['state']) ? $data['state'] : '');
        $list = $this->getStateList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsNameTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_name']) ? $data['is_name'] : '');
        $list = $this->getIsNameList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsSeeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_see']) ? $data['is_see'] : '');
        $list = $this->getIsSeeList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getIsDraftTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_draft']) ? $data['is_draft'] : '');
        $list = $this->getIsDraftList();
        return isset($list[$value]) ? $list[$value] : '';
    }


    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['create_time']) ? $data['create_time'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    //获取用户信息
    public function getuser($trends,$user_id){

    	$user = User::get($trends->user_id);

    	$data = [];
    	$data['nickname'] = $trends->is_name == 1?$user->anony:$user->nickname;
	    $data['avatar'] = $user->avatar;
	    //显示级别
	    $circle = Db::connect($this->connection)->name('circle_user')->where(['user_id'=>$trends->user_id,'circle_id'=>$trends->circle_id])->find();

	    if($circle){

		    $data['level'] = $circle['level'];
		    $level = Db::connect($this->connection)->name('publics_level')->where(['weigh'=>$circle['level']])->find();
		    $nickname = Db::connect($this->connection)->name('circle_level')->where(['level'=>$level['weigh'],'circle_id'=>$trends->circle_id])->value('nickname');
		    $data['level_name'] = $nickname?$nickname:$level['name'];
		    $data['icon'] = cdnurl($level['image'],true);
		    $data['bg_color'] = $level['bg_color'];
		    $data['font_color'] = $level['font_color'];
		    if($trends->user_id == $user_id && $circle['identity'] < 3){

			    $data['identity'] = $circle['identity'];

		    }else{
			    $data['identity'] = 0;
		    }

	    }else{

		    $data['level'] = 1;
		    $level = Db::connect($this->connection)->name('publics_level')->where(['weigh'=>1])->find();
		    $data['level_name'] = $level['name'];
		    $data['icon'] = cdnurl($level['image'],true);
		    $data['bg_color'] = $level['bg_color'];
		    $data['font_color'] = $level['font_color'];
		    $data['identity'] = 0;
	    }

     return $data;
    }

    protected function setCreateTimeAttr($value)
    {
        return $value === '' ? null : ($value && !is_numeric($value) ? strtotime($value) : $value);
    }



    public function user()
    {
        return $this->belongsTo('User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }

	public function circle()
	{
		return $this->belongsTo('Circle', 'circle_id', 'id', [], 'LEFT')->setEagerlyType(0);
	}

    //话题列表
	public function getHuatiListAttr($value, $data)
	{

		$list = json_decode(html_entity_decode($data['huati_text']),true);

		return $list ? $list : [];
	}

	//好友列表
	public function getHyListAttr($value, $data)
	{

		$list = json_decode(html_entity_decode($data['hy_text']),true);

		return $list ? $list : [];
	}


	/**
	 * 是否收藏
	 */
	public function getIsCollAttr($value, $data){

		$user_id = Auth::instance()->id ?: -1;

		$res = Db::connect($this->connection)->name("publics_likecoll")->where(['type'=>2,'user_id'=>$user_id,'trends_id'=>$data['id']])->find();

		return $res ? 1 : 2;

	}

	/**
	 * 是否点赞
	 */
	public function getIsLikeAttr($value, $data){

		$user_id = Auth::instance()->id ?: -1;

		$res = Db::connect($this->connection)->name("publics_likecoll")->where(['type'=>1,'user_id'=>$user_id,'trends_id'=>$data['id']])->find();

		return $res ? 1 : 2;

	}


     /*
      *
      *
      */
	public function getImagesUrlAttr($value, $data){

		$value = $value ?: $data["images"];
		return getImagesUrl($value);

	}

	public function getMp4FileUrlAttr($value, $data){

		$value = $value ?: $data["mp4_file"];
		return getImagesUrl($value);

	}


}
